package org.goldfish.library_system.controller;

import org.goldfish.library_system.entity.Reader;
import org.goldfish.library_system.service.ReaderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/readers")
public class ReaderController {
    @Autowired
    private ReaderService readerService;

    // 获取所有读者
    @GetMapping
    public List<Reader> getAllReaders() {
        return readerService.findAllReaders();
    }

    // 根据 ID 获取读者
    @GetMapping("/{id}")
    public Optional<Reader> getReaderById(@PathVariable Long id) {
        return readerService.findReaderById(id);
    }

    // 添加或更新读者
    @PostMapping
    public Reader addOrUpdateReader(@RequestBody Reader reader) {
        return readerService.saveReader(reader);
    }

    // 删除读者
    @DeleteMapping("/{id}")
    public void deleteReader(@PathVariable Long id) {
        readerService.deleteReader(id);
    }
}